﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using devtm.Collections;
using System.Xml.Linq;

namespace devtm.Editor.TypeSelector.AttributeBuilder
{

    public class ContextAttributeBuilder : ObservableCollection<ContextAttributeBuilderElement>
    {


        public ContextAttributeBuilder()
        {
            RootListe = new TableCollection<Type, string>(c => c.AssemblyQualifiedName);
        }


        public bool ModeSingle { get; set; }

        public ContextAttributeBuilderElement AddItem()
        {

            if (ModeSingle && this.Count > 1)
                return null;

            var i = new ContextAttributeBuilderElement(this);
            this.Add(i);
            return i;
        }


        public void RemoveItem(ContextAttributeBuilderElement item)
        {
            this.Remove(item);

        }


        internal TableCollection<Type, string> RootListe { get; set; }

        internal ViewCollection<Type, string> SubListe { get; set; }

        public void SubFilter(System.Linq.Expressions.Expression<Func<Type, bool>> filter)
        {
            if (SubListe != null)
                SubListe.Dispose();

            SubListe = RootListe.GetView(filter);

        }

        public void AddRange(IList<Type> list)
        {

            foreach (Type item in list)
            {
                if (!RootListe.Contains(item))
                    RootListe.Add(item);
            }

        }

        public void Add(Type type)
        {
            RootListe.Add(type);
        }


        internal XElement Serialize()
        {

            XElement ret = new XElement("attributes");

            foreach (ContextAttributeBuilderElement item in this.Items)
            {
                if (item.AttributeSelected.AttributeSelected != null)
                {
                    var c = item.AttributeSelected.Serialize();
                    if (c != null)
                        ret.Add(c);
                }
            }

            return ret;
        }


        internal void UnSerialize(XElement node)
        {

            foreach (XElement item in node.Elements())
            {
                ContextAttributeBuilderElement i = AddItem();
                i.AttributeSelected.UnSerialize(item);

                var ii = i.Header as TitleChooseAttributeUserControl;

                if (i.Root.Count > 1)
                    ii.ImgSuppression.Visibility = System.Windows.Visibility.Visible;

            }


        }


        public bool SelectType<T>(T attribute)
            where T : Attribute
        {

            ContextAttributeBuilderElement i = AddItem();
            bool ret = i.AttributeSelected.SelectType(attribute);

            var ii = i.Header as TitleChooseAttributeUserControl;

            if (i.Root.Count > 1)
                ii.ImgSuppression.Visibility = System.Windows.Visibility.Visible;

            return ret;

        }


    }
}
